//
//  BKGDMainBottomView.m
//  BESTKEEP
//
//  Created by 毛韶谦 on 2016/10/12.
//  Copyright © 2016年 YISHANG. All rights reserved.
//

#import "BKGDMainBottomView.h"
#import "BKGDCommon.h"

@implementation BKGDMainBottomViewItem

-(instancetype)initWithTitle:(NSString *)title width:(CGFloat)width type:(BKGDMainBottomViewItemType)type {
    self = [super init];
    if(self) {
        self.title = title;
        self.width = width;
        self.type = type;
    }
    return self;
}

@end


#define BKGDMainBottomViewCellBadgeRadius 7.5f

@interface BKGDMainBottomViewCell : UICollectionViewCell

@property(nonatomic,strong) NSString *badgeValue;

@property(nonatomic,strong,readonly) UILabel *titleLabel;
@property(nonatomic,strong,readonly) UILabel *badgeLable;

@end

@implementation BKGDMainBottomViewCell

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        [self initialize];
    }
    
    return self;
}

-(void)initialize {
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont systemFontOfSize:16];
    _titleLabel.textColor = [UIColor grayColor];
    _titleLabel.numberOfLines = 0;
    _titleLabel.userInteractionEnabled = NO;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:_titleLabel];
    
    _badgeLable = [[UILabel alloc] init];
    _badgeLable.hidden = YES;
    _badgeLable.font = [UIFont systemFontOfSize:8];
    _badgeLable.textColor = [UIColor whiteColor];
    _badgeLable.backgroundColor = [UIColor redColor];
    _badgeLable.textAlignment = NSTextAlignmentCenter;
    _badgeLable.layer.masksToBounds = YES;
    _badgeLable.layer.cornerRadius = BKGDMainBottomViewCellBadgeRadius;
    [self.contentView addSubview:_badgeLable];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    _titleLabel.frame = self.bounds;
}

-(void)setBadgeValue:(NSString *)badgeValue {
    _badgeValue = badgeValue;
    _badgeLable.text = _badgeValue;
    if(_badgeValue && ![_badgeValue isEqualToString:@""]) {
        _badgeLable.hidden = NO;
        [self layoutBadgeLabel];
    } else {
        _badgeLable.hidden = YES;
    }
}

-(void)layoutBadgeLabel {
    
    
    
    _badgeLable.frame = CGRectMake(CGRectGetWidth(self.frame) / 2 + 5, 4, BKGDMainBottomViewCellBadgeRadius * 2, BKGDMainBottomViewCellBadgeRadius * 2);
}

@end







NSString *const BKGDMainBottomViewCellIdentifier = @"BKGDMainBottomViewCellIdentifier";

#define BKGDMainBottomViewDefaultHeight 50

@interface BKGDMainBottomView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) CALayer *topLineLayer;

@property(nonatomic,strong) UIVisualEffectView *effectView;

@end

@implementation BKGDMainBottomView

-(instancetype)init {
    
//    self = [super initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
    
    self = [super init];
    
    if(self) {
        CGRect bounds = [[UIScreen mainScreen] bounds];
        self.frame = CGRectMake(0, 0, CGRectGetWidth(bounds), BKGDMainBottomViewDefaultHeight);
        [self initialize];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame {
    
    CGRect bounds = [[UIScreen mainScreen] bounds];
    frame.size = CGSizeMake(CGRectGetWidth(bounds), BKGDMainBottomViewDefaultHeight);
    
    self = [super initWithFrame:frame];
    if(self) {
        [self initialize];
    }
    
    return self;
}

-(void)initialize {
    
    UIView *bgView;
    
    if([[[UIDevice currentDevice] systemVersion] floatValue] > 8.0) {
        self.backgroundColor = [UIColor clearColor];
        _effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
        [self addSubview:_effectView];
        bgView = _effectView.contentView;
    } else {
        self.backgroundColor = [UIColor whiteColor];
        bgView = self;
    }
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.scrollEnabled = NO;
    _collectionView.backgroundColor = [UIColor clearColor];
    [_collectionView registerClass:[BKGDMainBottomViewCell class] forCellWithReuseIdentifier:BKGDMainBottomViewCellIdentifier];
    [bgView addSubview:_collectionView];
    
    _topLineLayer = [[CALayer alloc] init];
    _topLineLayer.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1.00].CGColor;
    [bgView.layer addSublayer:_topLineLayer];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    if(_effectView) {
        _effectView.frame = self.bounds;
    }
    _collectionView.frame = self.bounds;
    _topLineLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 1 / [UIScreen mainScreen].scale);
}

/**
 获取 购物车 收藏 已收藏 label
 */
-(NSAttributedString*)iconAttributedForType:(BKGDMainBottomViewItemType)type {
    
    UIImage *iconImage;
    NSString *text;
    CGRect imageBounds;
    
    if(type == BKGDMainBottomViewItemTypeShoppingCar) {
        iconImage = [UIImage imageNamed:@"BKGDShoppingCar"];
        text = [NSString stringWithFormat:@"\n%@", BKGDLocalizedString(@"BKGDMainBottomShoppingCar")];
        imageBounds = CGRectMake(0, 0, 30, 24);
        
    } else if(type == BKGDMainBottomViewItemTypeFavorite) {
        iconImage = [UIImage imageNamed:@"BKGDFavorite"];
        text = [NSString stringWithFormat:@"\n%@", BKGDLocalizedString(@"BKGDMainBottomFavorite")];
        imageBounds = CGRectMake(0, 0, 24, 24);
        
    } else if(type == BKGDMainBottomViewItemTypeFavoriteAlready) {
        iconImage = [UIImage imageNamed:@"BKGDFavoriteAlready"];
        text = [NSString stringWithFormat:@"\n%@", BKGDLocalizedString(@"BKGDMainBottomFavorite")];
        imageBounds = CGRectMake(0, 0, 24, 24);
    }
    
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = iconImage;
//    attachment.bounds = CGRectMake(0, 0, 30, 20);
    attachment.bounds = imageBounds;
    
    UIFont *textFont = [UIFont systemFontOfSize:10];
    UIColor *textColor = [UIColor blackColor];
    
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
    [attString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: textFont, NSForegroundColorAttributeName: textColor}]];
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 5;
    style.alignment = NSTextAlignmentCenter;
    
    [attString addAttributes:@{NSParagraphStyleAttributeName: style} range:(NSRange){0, attString.string.length}];
    
    return attString;
}

-(NSAttributedString*)titleAttributedForType:(BKGDMainBottomViewItemType)type title:(NSString*)title {
    NSString *text;
    UIColor *color;
    switch (type) {
        case BKGDMainBottomViewItemTypeAddShoppingCar: {
            text = BKGDLocalizedString(@"BKGDMainBottomAddShoppingCar");
            color = BKGDGreenColor;
        }
            break;
        case BKGDMainBottomViewItemTypeBuy: {
            text = BKGDLocalizedString(@"BKGDMainBottomBuy");
            color = BKGDMainColor;
        }
            break;
        case BKGDMainBottomViewItemTypeBid: {
            text = BKGDLocalizedString(@"BKGDMainBottomBid");
            color = BKGDMainColor;
        }
            break;
        case BKGDMainBottomViewItemTypeArrivalNotice: {
            text = BKGDLocalizedString(@"BKGDMainBottomArrivalNotice");
            color = BKGDYellowColor;
        }
            break;
        case BKGDMainBottomViewItemTypeDisabled: {
            text = BKGDLocalizedString(@"BKGDMainBottomBuy");
            color = BKGDDisabledColor;
        }
            break;
        case BKGDMainBottomViewItemStockUpPreOrder: {
            text = BKGDLocalizedString(@"BKGDMainBottomStockUpPreOrder");
            color = BKGDGreenColor;
        }
            break;
        default: {
            
        }
            break;
    }
    
    if(title) {
        text = title;
    }
    
//    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:text];
//    [attString addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15], NSForegroundColorAttributeName: [UIColor whiteColor], NSBackgroundColorAttributeName: color} range:(NSRange){0, attString.string.length}];
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor whiteColor], NSBackgroundColorAttributeName: color}];
    
    return attString;
}

-(void)setItems:(NSArray<BKGDMainBottomViewItem *> *)items {
    _items = items;
    [self reloadData];
}

#pragma -mark collectionView delegate

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if(_items) {
        return _items.count;
    }
    
    return 0;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BKGDMainBottomViewItem *item = _items[indexPath.row];
    CGSize size = CGSizeMake(item.width, BKGDMainBottomViewDefaultHeight);
    
    return size;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BKGDMainBottomViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:BKGDMainBottomViewCellIdentifier forIndexPath:indexPath];
    
    if([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
        [self configCell:cell atIndexPath:indexPath];
    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [self configCell:cell atIndexPath:indexPath];
}

-(void)configCell:(UICollectionViewCell*)cell atIndexPath:(NSIndexPath*)indexPath {
    BKGDMainBottomViewCell *btCell = (BKGDMainBottomViewCell*)cell;
    
    BKGDMainBottomViewItem *item = _items[indexPath.row];
    
    btCell.badgeValue = item.badgeValue;
    
    switch (item.type) {
        case BKGDMainBottomViewItemTypeShoppingCar:
        case BKGDMainBottomViewItemTypeFavorite:
        case BKGDMainBottomViewItemTypeFavoriteAlready: {
            btCell.titleLabel.attributedText = [self iconAttributedForType:item.type];
            btCell.titleLabel.backgroundColor = [UIColor clearColor];
        }
            break;
        default: {
            NSAttributedString *attString = [self titleAttributedForType:item.type title:item.title];
            
            //由于iOS7设置此属性会出现问题，所以不使用
            //btCell.titleLabel.attributedText = attString
            
            btCell.titleLabel.text = attString.string;
            btCell.titleLabel.textColor = [UIColor whiteColor];
            btCell.titleLabel.font = [UIFont systemFontOfSize:15];
            NSRange range;
            UIColor *backColor = item.backgroundColor;
            if(!backColor) {
                backColor = [attString attribute:NSBackgroundColorAttributeName atIndex:0 effectiveRange:&range];
            }
            btCell.titleLabel.backgroundColor = backColor;
        }
            break;
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BKGDMainBottomViewItem *item = _items[indexPath.row];
    
    if(_delegate) {
        [_delegate bottomView:self didSelectAtType:item.type];
    }
    
}

-(void)reloadData {
    [_collectionView.collectionViewLayout invalidateLayout];
    
    [_collectionView reloadData];
}

-(void)reloadAtIndex:(NSInteger)index {
//    [_collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]]];
    [_collectionView reloadData];
}

@end
